package com.yunfei.service;

import com.yunfei.bo.result.ResultModel;
import com.yunfei.bo.smrz.IdCardBo;
import com.yunfei.bo.smrz.SmrzBo;
import com.yunfei.enums.ResultStatus;
import com.yunfei.enums.ShiMingRenZhengStatus;
import com.yunfei.fegin.RedisService;
import com.yunfei.mapper.aiiidb.ShiMingRenZhengMapper;
import com.yunfei.utils.DbUtilCrm;
import com.yunfei.utils.idcard.AccountValidatorUtil;
import com.yunfei.utils.idcard.IDCardUtil;
import com.yunfei.utils.idcard.SmrzUtils;
import com.yunfei.utils.string.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: springclould_ylcaifu_App
 * @description:
 * @author: zhengyunfei
 * @create: 2019-01-21 13:13
 **/
@Service
public class ShiMingRenZhengService {
    @Autowired
    private RedisService redisService;
    @Autowired
    private ShiMingRenZhengMapper shiMingRenZhengMapper;
    @Autowired
    private Environment env;
    /**
     * @Description: 实名认证参数校验
     * @Param: [resultModel, cardType, cardNo]
     * @return: boolean
     * @Author: zhengyunfei
     * @Date: 2019/1/16
     * @time:13:28
     */
    public boolean validParamMethod(ResultModel resultModel, IdCardBo param) {
        //校验规则中国护照 9位  1个字母+8个数字   港澳通行证：9位，1个字母+8个数字   台胞证号是8位数字     国外人永久居留证：15位，前三位是字母
        String userName=param.getUserName();
        String cardType=param.getCardType();
        String cardNo=param.getCardNo();
        String cCustNo=param.getcCustNo();
        String email=param.getEmail();//邮箱
        System.out.println("email======================"+email);
        //非空校验
        if(StringUtil.isNullOrEmpty(cCustNo)){
            resultModel.setMessage(ShiMingRenZhengStatus.CUST_NO_NOTNULL.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.CUST_NO_NOTNULL.getCode());
            return true;
        }
        //邮箱格式错误
        if(!AccountValidatorUtil.isEmail(email)){
            resultModel.setMessage(ShiMingRenZhengStatus.EMAIL_ERRPR.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.EMAIL_ERRPR.getCode());
            return true;
        }
        if(StringUtil.isNullOrEmpty(cardType)){
            resultModel.setMessage(ShiMingRenZhengStatus.IDCARD_TYPE_ERROR.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.IDCARD_TYPE_ERROR.getCode());
            return true;
        }
        if(StringUtil.isNullOrEmpty(cardNo)){
            resultModel.setMessage(ShiMingRenZhengStatus.CARD_NO_ERROR.getMessage());
            resultModel.setCode(ShiMingRenZhengStatus.CARD_NO_ERROR.getCode());
            return true;
        }
        if("0".equals(cardType)){//身份证
            String errorMsg= IDCardUtil.IDCardValidate(cardNo);
            if(!StringUtil.isNullOrEmpty(errorMsg)){
                //身份证格式错误
                resultModel.setMessage(errorMsg);
                resultModel.setCode(ShiMingRenZhengStatus.IDCARD_NO_ERROR.getCode());
                return true;
            }
            /**校验身份证和姓名是否匹配
            SmrzBo result= SmrzUtils.getResult(userName,cardNo);
            if(!result.getStatus().equals("01")){
                System.out.println("实名认证返回结果====================="+result.getMsg());
               // resultModel.setMessage("姓名和身份证号不匹配");
                //resultModel.setCode(result.getStatus());
                resultModel.setMessage(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS_ERROR.getCode());
                //return true;
            }else{
                //param.setAreaAddress(result.getArea());
                //实名认证成功，同时更新实名认证的状态
                resultModel.setCode(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS_SUCCESS.getCode());
                resultModel.setMessage(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS_SUCCESS.getMessage());
            }
             **/
        }
        if("1".equals(cardType)){//中国护照
            boolean isHuZhao=cardNo.length()==9;
            if(!isHuZhao){
                resultModel.setMessage(ShiMingRenZhengStatus.HUZHAO_NO_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.HUZHAO_NO_ERROR.getCode());
                return true;
            }
        }
        if("A".equals(cardType)){//港澳通行证
            boolean isGangAoTongXingZheng=AccountValidatorUtil.isGangAoTongXingZheng(cardNo);
            if(!isGangAoTongXingZheng){
                resultModel.setMessage(ShiMingRenZhengStatus.GANGAOTONGXINGZHENG_NO_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.GANGAOTONGXINGZHENG_NO_ERROR.getCode());
                return true;
            }

        }
        if("D".equals(cardType)){//台胞证
            boolean isTaiBaoZheng=AccountValidatorUtil.isTaiBaoZheng(cardNo);
            if(!isTaiBaoZheng){
                resultModel.setMessage(ShiMingRenZhengStatus.TAIBAOZHENG_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.TAIBAOZHENG_ERROR.getCode());
                return true;
            }
        }
        if("J".equals(cardType)){//国外人永久居留证
            boolean isGaiWaiJuZhuZheng=AccountValidatorUtil.isGuoWaiJuZhuZheng(cardNo);
            if(!isGaiWaiJuZhuZheng){
                resultModel.setMessage(ShiMingRenZhengStatus.GAOWAIJUZHUZHENG_ERROR.getMessage());
                resultModel.setCode(ShiMingRenZhengStatus.GAOWAIJUZHUZHENG_ERROR.getCode());
                return true;
            }
        }
        if("7".equals(cardType)){//其他

        }

        return false;
    }
    //更新实名认证的状态
    public void updateSmrzStatus(ResultModel resultModel, String cCustNo,String cCustName) {
        //如果实名认证通过，则更新实名认证状态
        String code=resultModel.getCode()+"";
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("cCustNo",cCustNo);
        paramMap.put("cCustName",cCustName);
        if(code.equals(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS_SUCCESS.getCode()+"")){
            shiMingRenZhengMapper.updateSmrzStatusYiRenZheng(paramMap);
        }
        if(code.equals(ShiMingRenZhengStatus.SHI_MING_REN_ZHENG_STATUS_ERROR.getCode()+"")){
            //实名认证状态不通过
            shiMingRenZhengMapper.updateSmrzStatusWeiRenZheng(paramMap);
            shiMingRenZhengMapper.updateSmrzStatusBuTongGuo(paramMap);
        }
    }
    //获取数据库连接
    public Connection getConnection() {
        String url = env.getProperty("spring.datasource.crm.jdbcUrl");
        String username = env.getProperty("spring.datasource.crm.username");
        String password = env.getProperty("spring.datasource.crm.password");
        String driverClassName = env.getProperty("spring.datasource.crm.driverClassName");
        return DbUtilCrm.getCrmConnection(url, username, password, driverClassName);
    }
    public static void main(String args[]){
        System.out.println(AccountValidatorUtil.isEmail("sha_shai@163.com"));
    }
}
